﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using DTO;

namespace DataAccessTier
{
    public class NhaXuatBanDAO:DBConnection
    {
        public NhaXuatBanDAO() : base() { }

        public DataTable GetAllNhaXuatBan()
        {
            try
            {
                if (conn.State != ConnectionState.Open)
                    conn.Open();
                string sql = "SELECT * FROM NHAXUATBAN";
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                conn.Close();
                return dt;
            }
            catch (Exception)
            {
                conn.Close();
            }
            return null;
        }

        public bool kiemtratontai(string manxb)
        {
            if (conn.State != ConnectionState.Open)
                conn.Open();
            bool tatkt = false;
            SqlDataAdapter da_kiemtra = new SqlDataAdapter("SELECT * FROM NHAXUATBAN WHERE MANXB = '" + manxb + "'", conn);
            DataTable dt_kiemtra = new DataTable();
            da_kiemtra.Fill(dt_kiemtra);
            if (dt_kiemtra.Rows.Count > 0)
            {
                tatkt = true;
            }
            da_kiemtra.Dispose();
            conn.Close();
            return tatkt;
        }

        public bool Insert(NhaXuatBan nxb)
        {
            try
            {
                if (conn.State != ConnectionState.Open)
                    conn.Open();
                string sql = "INSERT INTO NHAXUATBAN(MANXB,TENNXB,DIACHI,SODT) VALUES('" + nxb.MANXB + "',N'" + nxb.TENNXB + "',N'" + nxb.DIACHI + "','" + nxb.SODT + "')";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.ExecuteNonQuery();
                conn.Close();
                return true;
            }
            catch (Exception)
            {
                conn.Close();
                return false;
            }
        }

        public bool Delete(string manxb)
        {
            try
            {
                if (conn.State != ConnectionState.Open)
                    conn.Open();
                string sql = "DELETE FROM NHAXUATBAN WHERE MANXB = '" + manxb + "'";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.ExecuteNonQuery();
                conn.Close();
                return true;
            }
            catch (Exception)
            {
                conn.Close();
                return false;
            }
        }

        public bool kiemtratontaiHH(string manxb)
        {
            if (conn.State != ConnectionState.Open)
                conn.Open();
            bool tatkt = false;
            SqlDataAdapter da_kiemtra = new SqlDataAdapter("SELECT * FROM HANGHOA WHERE MANXB = '" + manxb + "'", conn);
            DataTable dt_kiemtra = new DataTable();
            da_kiemtra.Fill(dt_kiemtra);
            if (dt_kiemtra.Rows.Count > 0)
            {
                tatkt = true;
            }
            da_kiemtra.Dispose();
            conn.Close();
            return tatkt;
        }

        public bool DeleteHH(string manxb)
        {
            try
            {
                if (conn.State != ConnectionState.Open)
                    conn.Open();
                string sql = "DELETE FROM HANGHOA WHERE MANXB = '" + manxb + "'";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.ExecuteNonQuery();
                conn.Close();
                return true;
            }
            catch (Exception)
            {
                conn.Close();
                return false;
            }
        }

        public bool Update(NhaXuatBan nxb)
        {
            try
            {
                if (conn.State != ConnectionState.Open)
                    conn.Open();
                string sql = "UPDATE NHAXUATBAN SET MANXB = '" + nxb.MANXB + "', TENNXB = N'" + nxb.TENNXB + "', DIACHI = N'" + nxb.DIACHI + "', SODT = '" + nxb.SODT + "' WHERE MANXB = '" + nxb.MANXB + "'";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.ExecuteNonQuery();
                conn.Close();
                return true;
            }
            catch (Exception)
            {
                conn.Close();
                return false;
            }
        }
    }
}
